//
//  UPShouCartViewController.m
//  LoveFreshBee
//
//  Created by wyzc on 16/6/17.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"SHOPCART"
#import "UPShouCartViewController.h"
#import "UPShopingCart.h"
#import "UPShopingCartMOdel.h"
#import "UPShopCartCell.h"
#import "HRZhiFuViewController.h"

@interface UPShouCartViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)UPShopingCart *shopingCart;
/**
 *  总价标签
 */
@property(nonatomic,weak)UILabel *priceLabel;

@property(nonatomic,weak) UIButton *selectedButton;

@end

@implementation UPShouCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航条名称
    self.navigationController.title=@"购物车";
    
    self.shopingCart=[UPShopingCart sharedCart];
    //注册cell
    
    UINib *nib=[UINib nibWithNibName:@"UPShopCartCell" bundle:nil];
    
    
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
    
    self.tableView.tableFooterView=[self createFooterView];
    
    
    NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(cellClick) name:@"CellClick" object:nil];
}

-(void)cellClick
{
    [self.tableView reloadData];
    int num=0;
    //获取选中的个数
    for (UPShopingCartMOdel *model in self.shopingCart.cart) {
        if (model.selected) {
            num++;
        }
    }
    self.selectedButton.selected=!(num==self.shopingCart.cart.count);
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark--tableview数据源代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.shopingCart.cart.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //从购物车获取cell所对应的模型
    UPShopCartCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    UPShopingCartMOdel *model=self.shopingCart.cart[indexPath.row];
    
    float price=[self.priceLabel.text floatValue];
    
    if (model.selected) {
        price+=model.price *model.num;

    }
    
    self.priceLabel.text=(indexPath.row==self.shopingCart.cart.count-1)?[NSString stringWithFormat:@"共￥%g",price]:[NSString stringWithFormat:@"%g",price];
    
    cell.model=model;
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(UIView *)createFooterView
{
    
    UIView *footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    //1.创建全选按钮
    
    UIButton *allSelectedBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [allSelectedBtn setBackgroundImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateNormal];
    [allSelectedBtn setBackgroundImage:[UIImage imageNamed:@"v2_noselected"] forState:UIControlStateSelected];
    UILabel *allSelectedName=[[UILabel alloc] initWithFrame:CGRectMake(44, 0, 44, 44)];
    allSelectedName.text=@"全选";
    //保存全选按钮
    self.selectedButton=allSelectedBtn;
    
    //添加全选按钮点击事件
    [allSelectedBtn addTarget:self action:@selector(allSelectClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:allSelectedBtn];
    [footerView addSubview:allSelectedName];
    //2.创建总价格标签
    UILabel *priceLabel=[[UILabel alloc] initWithFrame:CGRectMake(88, 0, 80, 44)];
    self.priceLabel=priceLabel;
    priceLabel.text=@"0";
    [footerView addSubview:priceLabel];
        //3.创建选好了按钮
    UIButton *rightBtn=[[UIButton alloc] initWithFrame:CGRectMake(280, 0,120 , 44)];
    [rightBtn setBackgroundColor:[UIColor yellowColor]];
    
    [rightBtn setTitle:@"选好了" forState:UIControlStateNormal];
    
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   //选好了按钮点击事件
    [rightBtn addTarget:self action:@selector(selectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [footerView addSubview:rightBtn];
    

    
    return footerView;
}

/**
 *  实现全选按钮的点击事件
 */
-(void)allSelectClick:(UIButton *)selectedButton
{
    selectedButton.selected=!selectedButton.selected;
    for (UPShopingCartMOdel *model in self.shopingCart.cart) {
        model.selected=!selectedButton.selected;
    }
    [self.tableView reloadData];
}

//实现选好了按钮点击事件
-(void)selectedBtnClick:(UIButton *)seletedBtn
{
    //UIButton *btn=seletedBtn;
    //设置选中后的颜色
//    [btn setBackgroundColor:[UIColor lightGrayColor]];
//    if (!self.shopingCart.cart.count) {
//        [btn setTitle:@"满￥0起送" forState:UIControlStateNormal];
    
   // }
    HRZhiFuViewController *zhiFu=[[HRZhiFuViewController alloc]init];
    [self.navigationController pushViewController:zhiFu animated:YES];
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    
    self.priceLabel.text=@"0";
    [self.tableView reloadData];
}
@end
